package com.supreme.businessserver.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.supreme.businessserver.mapper.mybatis.MBProductMapper;
import com.supreme.businessserver.mapper.mybatis.MBQualityMapper;
import com.supreme.commonentity.mysql.primary.BizProduct;
import com.supreme.commonentity.mysql.primary.BizQuality;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
@Slf4j
public class ProductQualityService {

    private final MBProductMapper mbProductMapper;
    private final MBQualityMapper mbQualityMapper;

    @Autowired
    public ProductQualityService(MBProductMapper mbProductMapper, MBQualityMapper mbQualityMapper) {
        this.mbProductMapper = mbProductMapper;
        this.mbQualityMapper = mbQualityMapper;
    }

    public List<BizProduct> getProductList() {
        QueryWrapper<BizProduct> queryWrapper = new QueryWrapper<>();
        return this.mbProductMapper.selectList(queryWrapper);
    }

    public List<BizQuality> getQualityList() {
        QueryWrapper<BizQuality> queryWrapper = new QueryWrapper<>();
        return this.mbQualityMapper.selectList(queryWrapper);
    }

    @Transactional()
    public void createProductQuality(BizProduct product, BizQuality quality, int status) {
        this.mbProductMapper.insert(product);
//        int result = 1 / 0;
//        log.error(String.valueOf(result));
        this.mbQualityMapper.insert(quality);

        if (status == 999) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }
}
